package com.tjetc.view;

import com.tjetc.domain.Doctor;
import com.tjetc.domain.Parent;
import com.tjetc.domain.Parents;
import com.tjetc.proxy.ClientProxy;
import com.tjetc.service.AdminService;
import com.tjetc.service.DoctorService;
import com.tjetc.service.ParentService;
import com.tjetc.view.manage.ParentMainView;
import com.tjetc.vo.AppDate;
import com.tjetc.vo.Result;

import java.util.Scanner;

public class MainView {
    private Scanner scanner=new Scanner(System.in);
    AdminService adminsService= ClientProxy.getProxyInstance(AdminService.class);
    DoctorService doctorService= ClientProxy.getProxyInstance(DoctorService.class);
    ParentService parentService=ClientProxy.getProxyInstance(ParentService.class);
    public void start() {
        while (true){
            System.out.println("1.管理员登录");
            System.out.println("2.医护人员登录");
            System.out.println("3.家长登录");
            System.out.println("4.家长注册");
            System.out.println("0.退出系统");
            System.out.println("输出0-4");
            try {
                int i = scanner.nextInt();
                switch (i){
                    case 1:
                        adminLogin();
                        break;
                    case 2:
                        doctorLogin();
                        break;
                    case 3:
                        parentLogin();
                        break;
                    case 4:
                        parentsRegist();
                        break;
                    case 0:
                        System.exit(0);
                    default:
                        break;
                }
            } catch (Exception e) {
                System.out.println("输入错误,请重新输入");
                scanner.nextLine();
            }
        }
    }
    private void parentLogin() {
        System.out.println("请输入用户名");
        String username = scanner.next();
        System.out.println("请输入密码");
        String password = scanner.next();
        Parent parent=new Parent();
//        Doctor doctor = new Doctor();
        parent.setUsername(username);
        parent.setPassword(password);
        Result result=parentService.findByNameAP(parent);
        System.out.println(result.getMsg());
        if (result.getCode()==200){
            AppDate.loginParent= (Parent) result.getData();
            new ParentMainView().start();
        }
    }
    private void parentsRegist() {
        System.out.println("请输入用户的姓名");
        String username = scanner.next();
        System.out.println("请输入密码");
        String password = scanner.next();
        System.out.println("请输入性别");
        String gender = scanner.next();
        String phone;
        while (true) {
            System.out.println("请输入手机号:");
            phone = scanner.next();
            if (!phone.matches("1[35789]\\d{9}")) {
                System.out.println("手机号格式不正确,请重新输入");
            } else {
                break;
            }
        }
        System.out.println("请输入地址");
        String addr = scanner.next();
        Parent parent = new Parent();
        parent.setUsername(username);
        parent.setPassword(password);
        parent.setGender(gender);
        parent.setPhone(phone);
        parent.setAddress(addr);
        Result result=parentService.add1(parent);
        System.out.println(result.getMsg());
    }
    private void adminLogin() {
        System.out.println("请输入管理员用户名");
        String  adminName  = scanner.next();
        System.out.println("请输入管理员密码");
        String  adminPassword = scanner.next();
        boolean login = adminsService.login(adminName, adminPassword);
        if (!login){
            System.out.println("用户名或密码错误");
            return;
        }else{
            System.out.println("登录成功");
            AdminMangeView adminMangeView = new AdminMangeView();
            adminMangeView.start();
        }
    }
    private void doctorLogin() {
        System.out.println("请输入用户名");
        String username = scanner.next();
        System.out.println("请输入密码");
        String password = scanner.next();
        Doctor doctor = new Doctor();
        doctor.setUsername(username);
        doctor.setPassword(password);
        Result result=doctorService.findByNameAP(doctor);
        System.out.println(result.getMsg());
        if (result.getCode()==200){
            AppDate.loginDoctor= (Doctor) result.getData();
            new DoctorMainView().start();
        }
    }
}
